package com.chaoyong.higo.fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.http.entity.StringEntity;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.chaoyong.higo.R;
import com.chaoyong.higo.activity.ProductDetailActivity;
import com.chaoyong.higo.adapter.NewsetContentAdapter2;
import com.chaoyong.higo.adapter.NewsetSortAdapter;
import com.chaoyong.higo.base.BaseFragment;
import com.chaoyong.higo.bean.TestNewsetBean;
import com.chaoyong.higo.bean.WiningGoodsBean;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.EmptyUtil;
import com.chaoyong.higo.utils.JLog;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

/**
 * 最新揭晓
 * 
 * @author zk
 *
 */
public class FragNewsAnnounced extends BaseFragment implements OnClickListener, OnItemClickListener {

	private static final String ClassName = FragNewsAnnounced.class.getName();

	private ImageView title_right_img;// 标题栏右侧图片
	private TextView title_right_text;// 标题栏右侧文字
	private TextView title_center_text;// 标题
	private ListView newest_lv;// 显示商品的listView
	private ListView sort_lv;// 排序的listView
	private NewsetContentAdapter2 adapter;// “最新揭晓”页面填充用的adapter
	private NewsetSortAdapter sortAdp;// “最新揭晓”页面中的排序填充用的adapter
	private List<TestNewsetBean> datas = new ArrayList<>();
	private boolean showSortLayout = true;
	
	private List<WiningGoodsBean.DataEntity.ListEntity> entities;
	private WiningGoodsBean bean;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mView = mInflater.inflate(R.layout.frag_news_announced, null);
		return mView;
	}

	@Override
	public void findView() {
		title_center_text = (TextView) mView.findViewById(R.id.title_center_text);
		title_right_img = (ImageView) mView.findViewById(R.id.title_right_img);
		title_right_text = (TextView) mView.findViewById(R.id.title_right_text);

		newest_lv = (ListView) mView.findViewById(R.id.newest_lv);

		//sort_lv = (ListView) mView.findViewById(R.id.sort_lv);
		//sort_lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		//sort_lv.setItemChecked(0, true);

		newest_lv.setOnItemClickListener(this);
		//sort_lv.setOnItemClickListener(this);
		
		Toast.makeText(mContext, "dfadsfa", 0).show();
		
	}

	@Override
	public void init() {
		title_center_text.setText("最新揭晓");
		title_right_img.setVisibility(View.INVISIBLE);
		title_right_text.setVisibility(View.INVISIBLE);
		title_right_text.setOnClickListener(this);

		adapter = new NewsetContentAdapter2(mContext, entities);
		newest_lv.setAdapter(adapter);

		sortAdp = new NewsetSortAdapter(mContext);
		//sort_lv.setAdapter(sortAdp);
	}

	@Override
	public void onClick(View v) {
		// showToast("11");
		/*if (!showSortLayout) {
			sort_lv.setVisibility(View.VISIBLE);
			newest_lv.setVisibility(View.GONE);
			showSortLayout = true;
		} else {
			sort_lv.setVisibility(View.GONE);
			newest_lv.setVisibility(View.VISIBLE);
			showSortLayout = false;
		}*/
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		switch (arg0.getId()) {
		case R.id.newest_lv:// 主布局的item点击事件
			 Intent intent = new Intent(mContext, ProductDetailActivity.class);
			 intent.putExtra("goods_id", entities.get(arg2).getGoods_id());
			 intent.putExtra("expect", entities.get(arg2).getExpect());
			 intent.putExtra("goods_img", entities.get(arg2).getCover_img().get(0));
			 intent.putExtra("head_img", entities.get(arg2).getUid_portrait());
			 intent.putExtra("price", entities.get(arg2).getGoods_price());
			 startActivity(intent);
			break;
		/*case R.id.sort_lv:// 排序方式
			sort_lv.setItemChecked(arg2, true);
			getSortText(arg1);
			break;*/
		}
	}

	private void getSortText(View arg1) {
		sort_lv.setVisibility(View.GONE);
		newest_lv.setVisibility(View.VISIBLE);
		showSortLayout = false;
		TextView newset_sort_tv = (TextView) arg1.findViewById(R.id.newset_sort_tv);
		String sortText = newset_sort_tv.getText().toString();
		title_right_text.setText(sortText);
	}

	Random random = new Random();

	private long getRandomTime() {
		long curTime = System.currentTimeMillis();
		long[] t = new long[] { 500, 1540, 2500 };
		return curTime + t[random.nextInt(3)] * 1000;
	}

	/*@Override
	public void onResume() {
		super.onResume();
		httpQueryNewAnnounced("8");
	}*/
	
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		httpQueryNewAnnounced("8");
	}

	private void httpQueryNewAnnounced(String items) {
		HttpUtils utils = new HttpUtils();
		RequestParams param = new RequestParams();
		try {
			JSONObject object = new JSONObject();

			object.putOpt("model", "GoodsWinning");
			object.putOpt("function", "getWinningAll");
			object.putOpt("code", "5,5,5");
			object.putOpt("app", 1);

			param.setBodyEntity(new StringEntity(object.toString(), "UTF-8"));
		} catch (Exception e1) {
			e1.printStackTrace();/*  */
		}
		utils.send(HttpMethod.POST, OApi.Public_appPort, param, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				showToast(arg1);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				JLog.i(ClassName, arg0.result);

				Gson gson = new Gson();
				bean = gson.fromJson(arg0.result, WiningGoodsBean.class);
				if (bean.getStatus().equals("1")) {
					entities = bean.getData().getList();
					// showToast("网络查询完毕"+ entities.size());
					if (!EmptyUtil.isEmpty(entities)) {
						adapter.changeDatas(entities);
					}
				}
			}
		});

	}

	/**
	 * 最新揭晓
	 */
	// private void httpQueryNewAnnounced() {
	// HttpUtils utils = new HttpUtils();
	// RequestParams param = new RequestParams();
	// try {
	// JSONObject object = new JSONObject();
	//
	// object.putOpt("model", "GoodsStatus");
	// object.putOpt("function", "getGoodsStatus");
	// object.putOpt("code", "0,4");
	// object.putOpt("app", 1);
	//
	// param.setBodyEntity(new StringEntity(object.toString(), "UTF-8"));
	// } catch (Exception e1) {
	// e1.printStackTrace();/* */
	// }
	// utils.send(HttpMethod.POST, OApi.Public_appPort, param, new
	// RequestCallBack<String>() {
	//
	// @Override
	// public void onFailure(HttpException arg0, String arg1) {
	// JLog.e(ClassName, arg0.getMessage());
	// }
	//
	// @Override
	// public void onSuccess(ResponseInfo<String> arg0) {
	// JLog.i(ClassName, arg0.result);
	// try {
	// JSONObject jsonObject = new JSONObject(arg0.result);
	// Gson gson = new Gson();
	// String status = jsonObject.getString("status");
	//
	// if (status.equals("1")) {
	// NewShareBean_z bean = gson.fromJson(arg0.result, NewShareBean_z.class);
	//
	// List<NewShareBean_z.DataEntity.ListEntity> entities =
	// bean.getData().getList();
	// if (!EmptyUtil.isEmpty(entities)) {
	// showToast(bean.getMessage() + "\n" + "list长度:" + entities.size());
	// adapter.addAllDataAndNorify(entities);
	// }else {
	//// showToast(bean.getMessage());
	// }
	// }
	// } catch (JSONException e) {
	// e.printStackTrace();
	// }
	//
	// }
	// });
	// }

}
